
var  organIds=$('#organIds').val();
$(document).ready(function () {		
	$("#date").datebox({onChange: function (n,o) {
		//arrears_custom();
		 loadBdctable();
		 loadBddtable();
		  xzgk();
		  other();
		  dutyProblem();
		  instrumentFailure();
		  hygiene();
			}}
	);
		//切换tabs 执行
		tabsSelect();
		//默认加载 不掉称顾客  table
		loadBdctable();
		
		// document.getElementById("saveBtn").style.display="none";
		
		
})	
//得到当前日期
	formatterDate = function(date) {
	var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
	var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0"
	+ (date.getMonth() + 1);
	return date.getFullYear() + '-' + month + '-' + day;
	};
	function loadBdctable(){	

		  document.getElementById("saveBtn").style.display="none";
	
		//不掉秤
		$('#bdc_custom').datagrid({
			  singleSelect:true,  
			  rownumbers: true, //显示序号
			  fitColumns:true,
			  pagination: true,
			  pageSize: 10,
			  url: "everyDaySession/getBdcCustomer",
		      queryParams: {
					
					'_query.organIds':organIds,
//					'_query.today':formatterDate(new Date())
					'_query.today':$('#date').datebox('getValue'),
					'_query.todayMorning':$('#date').datebox('getValue')+" 00:00:00"
				},
			  method: 'post',
			  columns:[[
						{field:'name',title:'姓名',width:110,align:'center'},
						{field:'phonenumber',title:'手机号码',width:110,align:'center'},
						{field:'laststoredate',title:'上次到店日期',width:110,align:'center'},
						{field:'initialweight',title:'最初体重（斤）',width:140,align:'center'},
						{field:'stantardweight',title:'标准体重（斤）',width:110,align:'center'},
						{field:'lastweight',title:'上次体重（斤）',width:110,align:'center'},
						{field:'bcweight',title:'本次体重（斤）',width:110,align:'center'},
						{field:'tolosepounds',title:'应减斤数（斤）',width:110,align:'center'},
						{field:'increasepounds',title:'涨秤斤数（斤）',width:110,align:'center',
							formatter: function(value,row,index)
				  			{
				  				var lastweight = row.lastweight;
				  				var bcweight = row.bcweight;
				  				
				  				if(lastweight!=null && bcweight!=null){
				  					var increasepounds=parseFloat(bcweight)-parseFloat(lastweight); 
				  					return  row.increasepounds=increasepounds.toFixed(1);
				  				}
				  				else{return row.increasepounds="0";}
				  			}
						}
			          ]]                                      		  
		});
	}

	/*tab选中事件
	 * 
	 */	
		function  tabsSelect()
		{
			 $('#daySession_tabs').tabs({  
				 	border:false,
			      onSelect:function(title){
			    	  if(title=="不掉秤顾客")
			    	  {	
			    		  loadBdctable();
			    	  }
			    	  if(title=="不到店顾客")
			    	  {	
			    		  loadBddtable();
			    		
			    	  }
			    	  
			    	  
			    	  
			    	  if(title=="欠款顾客")
			    	  {	
			    		  arrears_custom();
			    	  }
			    	  
			    	  
			    	  if(title=="产品库存")
			    	  {	
			    		  document.getElementById("saveBtn").style.display="none";
			    		//产品库存
			    			$('#product_stock').datagrid({
			    				  singleSelect:true,  
			    				  rownumbers: true, //显示序号
			    				  fitColumns:true,
			    				  pagination: true,
			    				  pageSize: 10,
			    				  url: "bstockHandover/getStock",
			    			      queryParams: {
			    			    	  '_query.sshorganIds':  $("#organIds").val(),
			    						'_query.sshjjDate': $("#handoverJjDate").val(),
			    						'_query.stockorganIds': $("#organIds").val(),
			    						'_query.kcstarttime': $("#handoverJjDate").val(),
			    						'_query.kcendtime': formatterDate(new Date()),
			    						'_query.kcorganIds': $("#organIds").val(),
			    						'_query.xsstarttime': $("#handoverJjDate").val(),
			    						'_query.xsendtime': formatterDate(new Date()),
			    						'_query.xsorganIds': $("#organIds").val(),
			    						'_query.lcIds': $("#lcIds").val()
			    					},
			    				  method: 'post',
			    				  columns:[[
			    				            {field:'productids',title:'产品ids', hidden:true},
			    							{field:'productname',title:'产品名称',width:150,align:'center'},
			    							{field:'typeids',title:'类别ids',hidden:true},
			    							{field:'productclass',title:'产品类别',width:100, align:'center'},
			    							{field:'stock',title:'库存',width:100, align:'center',
			    								formatter: function(value,row,index)
			    								{
			    									var stock = row.stock;
			    									if(stock!=null){ return  stock;}
			    									else{return row.stock="0";}
			    								}
			    							}
			    				          ]]                                      		  
			    			});
			    	  }
			    	  
			    	  
			    	  if(title=="其他问题顾客") {
			    		  other();
			    		}
			    	  if(title=="仪器故障"){
			    		  instrumentFailure();
			    		}
			    	  if(title=="卫生"){
			    		  hygiene();
			    	  }
			    	  if(title=="当班问题及处理办法"){
			    		  dutyProblem();
			    	  }
			    	  
			    	  
			    	  if(title=="新增顾客")
			    	  {	
			    		  xzgk();
			    	  }
			          
			      }   
			 });
			 
		}
	
	
		//查看顾客欠款详细信息
		function lookCustomerInfo(ids){
			
			//loadViewDialog('查看顾客欠款详细信息', 'everyDaySession/lookCustomerInfo?ids=' + ids);
			//欠款
			$('#arrears_custom_detail').datagrid({
				  singleSelect:true,  
				  rownumbers: true, //显示序号
				  fitColumns:true,
				  pagination: true,
				  pageSize: 10,
				  url: "everyDaySession/getArrearsCustomerDetail",
			      queryParams: {
			    	  	'_query.customerIds':ids,
			    	  	'_query.today':$('#date').datebox('getValue')
					},
				  method: 'post',
				  columns:[[
							{field:'name',title:'姓名',width:80,align:'center'},
							{field:'productname',title:'产品名称',width:150,align:'center'},
							{field:'number',title:'数量',width:50,align:'center'},
							{field:'unitprice',title:'单价',width:80,align:'center'},
							{field:'date',title:'日期',width:200,align:'center'},
							{field:'czy',title:'操作员',width:80,align:'center'},
							{field:'bz',title:'备注',width:200,align:'center'}
				          ]]                                      		  
			});
		}
		
		
		function arrears_custom(){
			  document.getElementById("saveBtn").style.display="none";
	    		//欠款
	    			$('#arrears_custom').datagrid({
	    				  singleSelect:true,  
	    				  rownumbers: true, //显示序号
	    				  fitColumns:true,
	    				  pagination: true,
	    				  pageSize: 10,
	    				  url: "everyDaySession/getArrearsCustomer",
	    			      queryParams: {
	    			    	  	'_query.organIds':organIds,
	    			    	  	'_query.today':$('#date').datebox('getValue')
	    					},
	    				  method: 'post',
	    				  columns:[[
	    							{field:'name',title:'姓名',width:80,align:'center',
	    								formatter: function(value,row,index){
										var ids = row.customerids;
										return '<a href="#" style="color: blue;" class="button-edit" onclick="lookCustomerInfo(\'' + ids + '\')">' + value + '</a>';
									}},
	    							{field:'phonenumber',title:'手机号码',width:150,align:'center'},
	    							{field:'receivable',title:'应收金额',width:80,align:'center'},
	    							{field:'proceeds',title:'实收金额',width:80,align:'center'},
	    							{field:'arrears',title:'欠款金额',width:80,align:'center'}
	    				          ]]                                      		  
	    			});
		}
		
		function loadBddtable(){
			  document.getElementById("saveBtn").style.display="none";
  			
  			//	document.getElementById("add").style.display="none";
  				
  		  //昨天
  		  var dd = new Date($('#date').datebox('getValue')); 
  		  dd.setDate(dd.getDate()-1);
  		  
  		//不到店
  			$('#bdd_custom').datagrid({
  				  singleSelect:true,  
  				  rownumbers: true, //显示序号
  				  fitColumns:true,
  				  pagination: true,
  				  pageSize: 10,
  				  url: "everyDaySession/getBddCustomer",
  			      queryParams: {
  			    	  	'_query.organIds':organIds,
  						'_query.yesterday': formatterDate(dd),
//  						'_query.today':formatterDate(new Date())
  						
  						'_query.today':$('#date').datebox('getValue')
  					},
  				  method: 'post',
  				  columns:[[
  							{field:'name',title:'姓名',width:110,align:'center'},
  							{field:'phonenumber',title:'手机号码',width:110,align:'center'},
  							{field:'laststoredate',title:'上次到店日期',width:110,align:'center'},
  							{field:'lastweight',title:'上次体重（斤）',width:110,align:'center'},
  							{field:'initialweight',title:'最初体重（斤）',width:140,align:'center'},
  							{field:'stantardweight',title:'标准体重（斤）',width:110,align:'center'},
  							{field:'tolosepounds',title:'应减斤数（斤）',width:110,align:'center'},
  							{field:'day',title:'不到店次数',width:110,align:'center',
  								formatter: function(value,row,index){
  									var sdate = new Date(row.laststoredate.replace(/-/g, "/"));
  									var now = new Date($('#date').datebox('getValue').replace(/-/g, "/")); 
  									var days = now.getTime() - sdate.getTime(); 
  									var day = parseInt(days / (1000 * 60 * 60 * 24)); 
  									return day; 
  								}}
  								
  				          ]]                                      		  
  			});
		}
		
		function xzgk(){
			 document.getElementById("saveBtn").style.display="none";
	    		//新增
	    		  	$('#new_custom').datagrid({
	    		  		  singleSelect:true,  
	    		  		  rownumbers: true, //显示序号
	    				  pagination: true,
	    				  pageSize: 10,
	    				  url: "everyDaySession/getXzCustomer",
	    			      queryParams: {
	    			    	  	'_query.organIds':organIds,
//	    						'_query.today':formatterDate(new Date())
	    			    	  	'_query.today':$('#date').datebox('getValue')
	    					},
	    				  method: 'post',
	    		  		  columns:[[
	    		  					{field:'name',title:'姓名',width:110,align:'center'},
	    		  					{field:'phonenumber',title:'手机号码',width:110,align:'center'},
	    		  					{field:'age',title:'年龄',width:110,align:'center'},
	    		  					{field:'height',title:'身高（cm）',width:110,align:'center'},
	    		  					{field:'weight',title:'体重（斤）',width:140,align:'center'},
	    		  					{field:'stantardweight',title:'标准体重（斤）',width:110,align:'center'},
	    		  					{field:'tolosepounds',title:'应减斤数（斤）',width:110,align:'center'},
	    		  					{field:'aim',title:'总目标',width:110,align:'center',
	    		  						formatter: function(value,row,index)
	    					  			{
	    					  				var aim = row.aim;
	    					  				if(aim != null){ return  aim;}
	    					  				else{return row.aim="0";}
	    					  			}
	    		  					},
	    		  					{field:'course',title:'购买疗程',width:110,align:'center'},
	    		  					{field:'coursenumber',title:'疗程数',width:110,align:'center'},
	    		  					{field:'money',title:'金额（元）',width:110,align:'center'},
	    		  					{field:'ispay',title:'是否支付',width:110,align:'center',
	    		  						formatter: function(value,row,index)
	    					  			{
	    					  				var ispay = row.ispay;
	    					  				if(ispay == 1){ return  row.ispay = "已支付";}
	    					  				else{return row.ispay="未支付";}
	    					  			}
	    		  					},
	    		  					{field:'acceptor',title:'接待人',width:110,align:'center'},
	    		  					{field:'feedback',title:'顾客反映',width:110,align:'center'},
	    		  					{field:'inshopmethod',title:'进店方式',width:110,align:'center'},
	    		  					{field:'attention',title:'注意事项',width:265,align:'center',editor:'textbox'}
	    		  		          ]]                                      		  
	    		  	});
		}
		
		function other(){
			 document.getElementById("saveBtn").style.display="";
    		 var date =  $('#date').datebox('getValue');
					
					if(date!="")
					{
						var url="/everyDaySession/getOther";
						var data={"date":date};
						
						var reData = common_ajax.ajaxFunc(url, data,"json");
	 					if(reData.msg!=200)
						{		
	 						 $('#otherCustomer').textbox('setValue',"");
						}else{
							  $('#otherCustomer').textbox('setValue',reData.other.problemcustomer);
					}
    	  }
		}
		function dutyProblem (){
			 document.getElementById("saveBtn").style.display="";
   		  var date =  $('#date').datebox('getValue');
				
				if(date!="")
				{
					var url="/everyDaySession/getOther";
					var data={"date":date};
					
					var reData = common_ajax.ajaxFunc(url, data,"json");
					if(reData.msg!=200)
					{		
						 $('#dutyProblem').textbox('setValue',"");
					}else{
						  $('#dutyProblem').textbox('setValue',reData.other.dutyproblem);
				}
   	  }
		}
		
		function instrumentFailure(){
			  
  		  document.getElementById("saveBtn").style.display="";
  		  var date =  $('#date').datebox('getValue');
				
				if(date!="")
				{
					var url="/everyDaySession/getOther";
					var data={"date":date};
					
					var reData = common_ajax.ajaxFunc(url, data,"json");
					if(reData.msg!=200)
					{		
						 $('#instrumentFailure').textbox('setValue',"");
					}else{
						  $('#instrumentFailure').textbox('setValue',reData.other.instrumentfailure);
				}
  	  }
		}
		
		function hygiene(){
			 document.getElementById("saveBtn").style.display="";
   		  var date =  $('#date').datebox('getValue');
				
				if(date!="")
				{
					var url="/everyDaySession/getOther";
					var data={"date":date};
					
					var reData = common_ajax.ajaxFunc(url, data,"json");
					if(reData.msg!=200)
					{		
						 $('#hygiene').textbox('setValue',"");
					}else{
						  $('#hygiene').textbox('setValue',reData.other.hygiene);
				}
   	  }
		}
		
	//保存
	function  save()
	{	
		var url='/everyDaySession/saveDaySession';
		var data=$('#rihui_form').serialize();
		var reData = common_ajax.ajaxFunc(url,data);
		if(reData == "200")
		{
			 $.slideMsg("保存成功", parent.$);
			 parent.refresh();
		}else
		{
			$.alert(parent.$, reData);
		}
		
	}
	$(document).keydown(function(event){ 
		if(event.keyCode == 13){
			$('#bdc_custom,#bdd_custom,#new_custom,#qt_custom').datagrid("endEdit", editIndex1);
		} 
	});

	
	
	
